#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>

typedef struct{
    long mtype;
    char mtext[64];
}MSG;

#define LEN (sizeof(MSG) - sizeof(long))

#define TypeA 100

#define TypeB 200


int main(){
    key_t key;
    int msg_id;
    key = ftok(".",'q');
    if(key == EOF){
        perror("ftok");
        exit(1);
    }

    msg_id = msgget(key, IPC_CREAT|0666);
    if(msg_id == -1){
        perror("msgget");
        exit(1);
    }

    while(1){
        printf("please input your msg to send\n");
        MSG msg;
        msg.mtype = TypeB;
        fgets(msg.mtext, 64, stdin);
        if(msgsnd(msg_id, &msg, LEN, 0) == -1){
            perror("msgsnd");
            exit(1);
        }

        if(msgrcv(msg_id, &msg, LEN, TypeA, 0) == -1){  
            perror("msgrcv");
            exit(1);
        }

        printf("clientA Receive: %s", msg.mtext);

    }
    return 0;
}